function [KA,Weights,name] = HoughSelfcalib1_svdopt(TF,w,h,numparams,corrs)
%this function , given a camera center and a focal length and a fundamental
%matrix computes the error with respect to a fundamental matrix
if(numparams~=2)
   error('function only works with 2 parameters, aspect ratio and focal length'); 
end
if (nargin == 1)
    w=512;
    h=512;
end
%TF=TF*10000;
plotting=1;
name='clustering norm';
numclusts=20;
[~,sizeFs]=size(TF);


numtries=200;  % we want to have 400 points in the end


ffinals1=zeros(numtries,sizeFs);
ffinals2=zeros(numtries,sizeFs);

Weights=ones(1,1);

 


for q=1:sizeFs
    
    clear allsols

        [allsols,  ~,~] =nonlinearOptimizeselfcalibnormMODREDUCEDTIME(cF, w,h, numparams ,Weights,numtries);
%     plot(allsols(:,2),allsols(:,1))
%     figure
    for p=1:size(allsols,1)
        
      ffinals1(p,q)=allsols{1,p}(1,1);
      ffinals2(p,q)=allsols{1,p}(2,2);       
        
    end
    
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%5


XComplete=[ reshape(ffinals1,sizeFs*numtries,1)   reshape(ffinals2,sizeFs*numtries,1)  ];

if(plotting==1)
    [newsizepts,n]=size(XComplete);
    fff=zeros(newsizepts,3);
    
    for k=1:sizeFs
        scolors{k}=rand(1,3);
    end
    
    %remove this later
    scolors{1}=[0.9 0.0 0.0];
    scolors{2}=[0.0 0.9 0.0];
    scolors{3}=[0.0 0.0 0.9];
    
    
    count=1;
    for k=1:sizeFs
        for m=1:numtries
            
            fff2(count,:)=  scolors{k};
            
            count=count+1;
        end
    end
    
    
    scatter(XComplete(:,2),XComplete(:,1),10,fff2,'filled');
    title(['scatter plot of all the solution points']);

    xlabel('focal length 2');
    ylabel('focal length 1');
end


%%%%%%%%%%%
%creating an index of the memberships
mcount=1;

for q=1:sizeFs
    for i=1:numtries
        idx_membership(mcount,1)=q;
        mcount=mcount+1;
    end
end

mcount=1;
ccounter=1;


[newsizepts,n]=size(XComplete);


[idx,ctrs,sumd] = kmeans(XComplete,numclusts,'Replicates',5,'emptyaction','drop','display','off');








classscores=zeros(numclusts,sizeFs);

%this is a dummy variable helping to debug this thing, the columns contain
%some attributes of the classes , i wont need this in the production
%version
objscores=zeros(numclusts,3);


for j=1:newsizepts
    if(idx_membership(j)~=0)
        classscores(idx(j),idx_membership(j))=classscores(idx(j),idx_membership(j))+1;
    end
end

%look here an fix it
numMaxima=2;
Iold=2;
I=1;
numvotes=0;
maxcounter=newsizepts;
while(numMaxima~=1)
    maxcounter=maxcounter-1;
    Iold=I;
    numvotes=numvotes+1;
    clear    objscores C;
    objscores(:,1)= sum(classscores>numvotes,2)   ;
    [C,I] = max(objscores);
    numMaxima=sum(objscores==C,1) ;
    if(maxcounter==0)
        numMaxima=1;
        [C,I] = max(sum(classscores,2));
        break;
    end
end

if(numMaxima~=1)
    I=Iold;
end





bestf1  = ctrs(I,1);
bestf2  = ctrs(I,2);
%
if(plotting==1)
    
    figure
    hist(reshape(ffinals1,sizeFs*numtries,1),numtries/2);
    title(['focal length 1']);
    
      figure
     hist(reshape(ffinals2,sizeFs*numtries,1),numtries/2);
    title(['focal length 2']);
    
    
    
    
    fff=zeros(newsizepts,3);
    for k=1:numclusts
        scolors{k}=rand(1,3);
    end
    
    
    
    figure
    for k=1:newsizepts
        if(idx(k,1)~=I)
            fff(k,:)=  scolors{idx(k,1)};
        else
            fff(k,:)=  [0 0 0];
        end
    end
    
    
    
    
    subplot(1,2,1)
    
    scatter(XComplete(:,2),XComplete(:,1),10,fff,'filled');
    title(['new scatter plot of all the results found using my method, winning cluster, ' num2str(I) ' in black']);
    xlabel('focal length 2');
    ylabel('focal length 1');
    
      subplot(1,2,2) 
    scatter(XComplete(:,2),XComplete(:,1),10,fff2,'filled');
    title(['scatter plot of all the solution points']);
    xlabel('focal length 2');
    ylabel('focal length 1');
 

    
end



KA=[bestf1 0 w/2 ; 0 bestf2 h/2 ; 0 0 1];


end
